package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.BaseDomainConvert;
import com.one.blocks.rbac.controller.convert.worker.SysOrgQualifier;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.query.SysOrgQuery;
import com.one.blocks.rbac.controller.vo.SysOrgVo;
import com.one.blocks.rbac.controller.vo.tree.SysOrgTreeNodeVo;
import com.one.blocks.rbac.domain.SysOrg;
import com.one.blocks.security.util.SubjectHelper;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_TIME;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_ID;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_TIME;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Mapper(componentModel = "spring", uses = {SysOrgQualifier.class, TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysOrgConvert extends BaseDomainConvert<SysOrg, SysOrgVo, SysOrgQuery> {

    /**
     * 数据对象转换
     *
     * @param sysOrgVo 前端参数对象
     * @return 数据对象
     */
    @Named(value = "voToEntityIgnoreId")
    @Mapping(target = CONVERT_ID, ignore = true)
    @Mapping(target = CONVERT_CREATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = CONVERT_UPDATE_BY, ignore = true)
    @Mapping(target = CONVERT_UPDATE_TIME, ignore = true)
    @BeanMapping(qualifiedByName = {"sysOrgQualifier"})
    @Override
    SysOrg voToEntityIgnoreId(SysOrgVo sysOrgVo);

    /**
     * 更新参数转换
     *
     * @param sysOrgVo 前端参数
     * @return 业务封装对象
     */
    @Named(value = "voToEntity")
    @Mapping(target = CONVERT_CREATE_BY, ignore = true)
    @Mapping(target = CONVERT_CREATE_TIME, ignore = true)
    @Mapping(target = CONVERT_UPDATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @BeanMapping(qualifiedByName = {"sysOrgQualifier"})
    @Override
    SysOrg voToEntity(SysOrgVo sysOrgVo);

    /**
     * 转换为树节点
     *
     * @param sysOrg 源数据
     * @return 树节点
     */
    SysOrgTreeNodeVo toTreeNode(SysOrg sysOrg);

    /**
     * 转换为树结构
     *
     * @param sysOrgList 源数据
     * @return 树结构
     */
    List<SysOrgTreeNodeVo> toTreeNodeList(List<SysOrg> sysOrgList);
}